//
//  SearchView.m
//  EducationAdminClient
//
//  Created by KWSD on 16/1/11.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "SearchView.h"
#import "SearchCell.h"
#import "Masonry.h"
#import "NetRequest.h"
#import "EducationAdminClient_URL.h"
//#import "OneSelectView.h"
#import "TimeSelectView.h"
#import "NormalButton.h"
#import "SelectManyView.h"
#import "SearchViewController.h"
#import "CustomTextField.h"
#import "NormalText.h"


//#import "ListsViewController.h"

#define TABLEVIEWHEADERHEIGHT 40


#define LEFT @"LEFT"
#define RIGHT @"RIGHT"
#define NORMAL @"NORMALTEXT"

@interface SearchView ()
@property(nonatomic,strong)UITextField *oneText;
//@property(nonatomic,strong)OneSelectView *seleView;
@property(nonatomic,strong)SelectManyView *seleView;

@property(nonatomic,strong)UIImageView  *seleViewBg;

//@property(nonatomic,strong)UIImageView   *seleViewBg;
@property(nonatomic,strong)UIImageView   *timeSelectViewBg;

@property(nonatomic,strong)NSMutableArray * dicArr;
@end

@implementation SearchView
{
    TimeSelectView     *timeSelectView;
    NormalButton       *currentBtn;         // time btn reach
    BOOL               shouldClear;         // clear btn need response
    NormalText         *lastTextField;      // remember the last textField that edited to resignFirst Response for next
}
-(instancetype)initWithBlock:( void(^)(NSDictionary *dic) )cb{
    
    
    if(self = [super init]){
        
        
        
        _cb = cb;
        
        self.backgroundColor = [UIColor whiteColor];
        shouldClear = NO;
        
        [self creatUI];
    }
    return self;
}

-(void)creatUI{
    
    _dicArr = [[NSMutableArray alloc]init];
     _param = [[NSMutableDictionary alloc]init];
    
    _tableView = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStyleGrouped];
    [self addSubview:_tableView];
//    _tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0);
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@(0));
    }];

    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.0001)];
    _backBtn = [[UIButton alloc]init];
    [self addSubview:_backBtn];
    
//    _backBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2+10, 0, [UIScreen mainScreen].bounds.size.width/2-20, 40);
    
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/2+10));
        make.top.equalTo(self.mas_bottom);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/2-20));
        make.height.equalTo(@(40));
    }];

    
    _backBtn.backgroundColor = [UIColor colorWithHex:0x56d176];
    [_backBtn setTitle:@"确定" forState:UIControlStateNormal];
    _backBtn.layer.cornerRadius = 4;
    [_backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _clearBtn = [[UIButton alloc]init];
    [self addSubview:_clearBtn];
    
    

//    _clearBtn.frame = CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width/2-20, 40);
    
    [_clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(self.mas_bottom);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/2-20));
        make.height.equalTo(@(40));
    }];

    _clearBtn.backgroundColor = [UIColor whiteColor];
    [_clearBtn setTitle:@"清空" forState:UIControlStateNormal];
    _clearBtn.layer.cornerRadius = 4;
    _clearBtn.layer.borderColor = [[UIColor colorWithHex:0xe0e0e0]CGColor];
    _clearBtn.layer.borderWidth = 1;
    [_clearBtn setTitleColor:[UIColor colorWithHex:0x5c5c5c] forState:UIControlStateNormal];
    [_clearBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    _seleView =[[SelectManyView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-150, 40, 300, 300)];
    _seleViewBg = [[UIImageView alloc]init];
    [self addSubview:_seleViewBg];
    [_seleViewBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
    }];
    _seleViewBg.userInteractionEnabled = YES;
    _seleViewBg.backgroundColor = [UIColor blackColor];
    _seleViewBg.alpha = 0.5;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [_seleViewBg addGestureRecognizer:tap];
    _seleViewBg.userInteractionEnabled = YES;

    [self addSubview:_seleView];
    
    [_seleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/2-150));
        make.top.equalTo(@(40));
        make.width.equalTo(@(300));
        make.height.equalTo(@(300));
    }];

    _seleView.hidden = YES;
    _seleViewBg.hidden = _seleView.hidden;
    
//    _seleView.seleSearchView = self;
    _seleView.delegateView = self;

}



-(void)tapClick{
    NSLog(@"222");
    _seleView.hidden = YES;
    _seleViewBg.hidden = YES;
}

#pragma mark - clearBtnClick remove all obj
-(void)clearBtnClick{
    [_param removeAllObjects];
    shouldClear = YES;
    for (int i = 0; i<_dicArr.count; i++) {
        [_dicArr replaceObjectAtIndex:i withObject:@""];
    }
    [_tableView reloadData];
//    shouldClear = NO;
    
}
-(void)settimerView:(UIView *)view{
    
    if(lastTextField)[lastTextField resignFirstResponder];

    if (view) {
        timeSelectView = [[TimeSelectView alloc]init];
        
        
        _timeSelectViewBg = [[UIImageView alloc]init];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userHeidden)];
        [_timeSelectViewBg addGestureRecognizer:tap];
        _timeSelectViewBg.userInteractionEnabled = YES;
        
        [self addSubview:_timeSelectViewBg];
        [_timeSelectViewBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.top.equalTo(self.mas_top);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.mas_bottom);
        }];
        _timeSelectViewBg.userInteractionEnabled = YES;
        _timeSelectViewBg.backgroundColor = [UIColor blackColor];
        _timeSelectViewBg.alpha = 0.5;

        timeSelectView.hidden = YES;
        
        _timeSelectViewBg.hidden = timeSelectView.hidden;
        
        timeSelectView.delegateView = self;
        
        [view addSubview:timeSelectView];
        
         [view bringSubviewToFront:timeSelectView];
        
        [timeSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(view.mas_bottom);
            make.left.equalTo(view.mas_left);
            make.right.equalTo(view.mas_right);
            make.height.equalTo(@(342/2));
        }];
        
    }

}
-(void)backClick:(UIButton *)btn{
    
    if(lastTextField){
        NSInteger i =lastTextField.indexPath.row;
        NSDictionary *d = [_fieldArr objectAtIndex:i];
        NSString *p3 = [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:[d objectForKey:@"fieldType"]]];
        if(![_param valueForKey:p3])[_param setObject:lastTextField.text forKey:p3];
        
        //*** setting search
        NSString *p1 = [NSString stringWithFormat:@"%@_%@_andOr",[d objectForKey:@"fieldAliasName"],[d objectForKey:@"fieldType"]];
     
        NSString *p2;
        if(![lastTextField isKindOfClass:[CustomTextField class]]){
        
        p2= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:[d objectForKey:@"fieldType"]]];
        }else{
            p2= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:@"5"]];
        }
        if(![_param objectForKey:p1])[_param setObject:@"0" forKey:p1];
        if(![_param objectForKey:p2])[_param setObject:@"0" forKey:p2];
        [lastTextField resignFirstResponder];
    }
    [self dataRequest];
}

-(void)setSearch:(NSDictionary *)dic{//AFM_22_numValOne_pld=32  FM_22_numValTwo_pld=55
    //*** setting search
    
//    if(![dic valueForKey:@"change"]){
    NSString *p1 = [NSString stringWithFormat:@"%@_%@_andOr",[dic objectForKey:@"fieldAliasName"],[dic objectForKey:@"fieldType"]];
    NSString *p2;
    if(![dic valueForKey:@"right"]){
    
        p2= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr:[dic objectForKey:@"fieldType"]]];
    }else{
        p2= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr:@"5"]];
    }
    if(![_param objectForKey:p1])[_param setObject:@"0" forKey:p1];
    [_param setObject:[NSString stringWithFormat:@"%@", [dic valueForKey:@"content"]] forKey:p2];
    if([[dic valueForKey:@"fieldType"] integerValue] == 1){ // 由于后台我设置条件就会出错，所以一旦点了就得设置条件
            NSString *pForNumClud;
            NSString *pForNumPlaceHo;
            NSString *pForPlaceHo;
            NSString *p3;
            pForNumClud= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr:[dic objectForKey:@"fieldType"]]];
            pForNumPlaceHo= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr:@"5"]];
            p3= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr2:[dic objectForKey:@"fieldType"]]];
        
            pForPlaceHo= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr2:@"99999"]];
        if (![_param valueForKey:pForNumClud]) {
            [_param setObject:@"" forKey:pForNumClud];
        }
        
        if (![_param valueForKey:pForNumPlaceHo]) {
            [_param setObject:@"" forKey:pForNumPlaceHo];
        }
        if (![_param valueForKey:pForPlaceHo]) {
            [_param setObject:@"" forKey:pForPlaceHo];
        }
        if (![_param valueForKey:p3]) {
            [_param setObject:@"" forKey:p3];
        }

    }
    
    else{
       NSString *p3= [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr2:[dic objectForKey:@"fieldType"]]];   // 平常情况下 ， 左边墙框下 键
        if (![_param valueForKey:p3]) {
            [_param setObject:@"" forKey:p3];
        }
    }
//    }else{
//        
//        NSString *p1 = [NSString stringWithFormat:@"%@_%@_andOr",[dic objectForKey:@"fieldAliasName"],[dic objectForKey:@"fieldType"]];
//        NSString *p2 = [NSString stringWithFormat:@"%@%@",[dic objectForKey:@"fieldAliasName"],[self typeStr2:[dic objectForKey:@"fieldType"]]];
//        if(![_param objectForKey:p1])[_param setObject:@"0" forKey:p1];
//        [_param setObject:[NSString stringWithFormat:@"%@", [dic valueForKey:@"content"]] forKey:p2];
//
//    }
}
-(void)dataRequest{
    
//    [_param setObject:_tableID forKey:@"tableId"];
//    [_param setObject:_pageID forKey:@"pageId"];
    NSArray *arr = [_searchUrl componentsSeparatedByString:@"?"];
     NSString *requestStr =@"";
    NSArray *arr2 = [arr[1] componentsSeparatedByString:@"&"];
    NSArray *array1 = [arr2[0] componentsSeparatedByString:@"="];
     NSArray *array2 = [arr2[1] componentsSeparatedByString:@"="];
    if(arr!=nil){
        requestStr =arr[0];
    [_param setObject:array1[1] forKey:array1[0]];
    [_param setObject:array2[1] forKey:array2[0]];
    }
    else{
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        if([userDefault valueForKey:PROGRAMA])_programaUrl = [NSString stringWithFormat:@"http://%@:%@/edus_auto/", [userDefault valueForKey:PROGRAMA], [userDefault valueForKey:PORT]];
        
        NSString *str = [userDefault valueForKey:PROGRAMA];
        if (str.length<=0) {
            self.programaUrl = getBase();
        }

        requestStr = [NSString stringWithFormat:@"%@%@", self.programaUrl, LISTSVIEWREQUEST_URL];
        [_param setObject:userName() forKey:@"loginName"];
        [_param setObject:passWord() forKey:@"password"];
        if(_tableID)[_param setObject:_tableID forKey:@"tableId"];
        if(_pageID)[_param setObject:_pageID forKey:@"pageId"];
    }
    


    
    NSLog(@"%@",_param);
    [NetRequest requestUrl:requestStr andParamas:_param andReturnBlock:^(NSData *data, NSError *error) {
        if(!error&&data!=nil){
            
            
            NSLog(@"%@",data);
            NSDictionary *d =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"%@",d);
            _rowArr  =[d objectForKey:@"dataList"];
            
            
             _cb(_param);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"row" object:_rowArr];
            
        }else{
            _rowArr = [[NSArray alloc]init];
            
             _cb(_param);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"row" object:_rowArr];

            NSString *str = [NSString stringWithFormat:@"%@", error.localizedDescription];
            NSLog(@"%@", str);
        
        }
        
       
        
       // if(self.currentShowView){
         //   if([self.currentShowView isKindOfClass:[SearchViewController class]]){
           //     SearchViewController *seVC = (SearchViewController *)self.currentShowView;
             //   if([seVC.receiveParama isKindOfClass:[ListsViewController class]]){
               //     ListsViewController *listVC = (ListsViewController *)seVC.receiveParama;
                 //   listVC.searchDic = _param;
                //}
            //}
        //}

        if(self.currentShowView)
            [self.currentShowView dismissViewControllerAnimated:YES completion:^{
                
            }];
        
    }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100/2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _fieldArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *inden = @"identifier";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:inden];
    if(cell==nil){
        cell =[[SearchCell alloc]initIdentifier:inden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    if (indexPath.row == 1) {
        NSLog(@"1111");
    }
    
    NSDictionary *dic = _arrSelected[indexPath.row];
    
//    [_dicArr addObject:@""];
    NSDictionary *d = [_fieldArr objectAtIndex:indexPath.row];
    cell.la.text = [NSString stringWithFormat:@"%@", [d objectForKey:@"fieldCnName"]];
    cell.text.tag = indexPath.row+1000;
    cell.text.indexPath = indexPath;
    cell.textLeft.indexPath = indexPath;
    cell.textRight.indexPath = indexPath;

    cell.textLeft.tag = indexPath.row+4000;
    cell.textRight.tag = indexPath.row+5000;

    cell.text.delegate = self;
    
//    if(![[_dicArr objectAtIndex:indexPath.row] isEqualToString:@""]){
//        cell.text.text =[_dicArr objectAtIndex:indexPath.row];
//    }
    
    //*** 类似MVC
    cell.dic = d;
    
    //*** setting delegate View for time and content
    cell.delegateView = self;

    NSString *fieldRole=[NSString stringWithFormat:@"%@",[d objectForKey:@"fieldRole"]];
    NSString *fieldtype=[NSString stringWithFormat:@"%@",[d objectForKey:@"fieldType"]];
    if([fieldRole isEqualToString:@"16"]||[fieldtype isEqualToString:@"3"]){
        cell.text.enabled = NO;
        cell.textRight.enabled = NO;

        if([fieldtype isEqualToString:@"3"]){ //*** 日期格式
            
            [cell updateLayOutForTime];
            cell.startDay.hidden = NO;
            cell.endDay.hidden = NO;
            cell.lineImg.hidden = NO;
            cell.startDay.indexPath = indexPath;
            cell.endDay.indexPath = indexPath;
            
            cell.startDay.tag = indexPath.row+2000;
            cell.endDay.tag = indexPath.row +3000;
//            cell.delegateView = self;
            
            if ([dic valueForKey:LEFT]) {
                [cell.startDay setTitle:[NSString stringWithFormat:@"%@", [dic valueForKey:LEFT]] forState:UIControlStateNormal];
            }else{
                [cell.startDay setTitle:@"请选择" forState:UIControlStateNormal];
            }
            
            if ([dic valueForKey:RIGHT]) {
                [cell.startDay setTitle:[NSString stringWithFormat:@"%@", [dic valueForKey:RIGHT]] forState:UIControlStateNormal];
            }else{
                [cell.endDay setTitle:@"请选择" forState:UIControlStateNormal];
            }

            if (shouldClear) {
                [cell.startDay setTitle:@"请选择" forState:UIControlStateNormal];
                [cell.startDay setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
                
                [cell.endDay setTitle:@"请选择" forState:UIControlStateNormal];
                [cell.endDay setTitleColor:[UIColor colorWithRed:0x5d/255.0 green:0xa6/255.0 blue:0xff/255.0 alpha:1] forState:UIControlStateNormal];
            }

        }
        else{
//            cell.startDay.hidden = YES;
//            cell.endDay.hidden = YES;
//            cell.lineImg.hidden = YES;
//            
//            //*** 左右
//            cell.cluCenter.hidden = YES;
//            cell.textLeft.hidden = YES;
//            cell.textRight.hidden = YES;
//            cell.clu1.hidden = YES;
            [cell updateLayOutForNormal];
            
            if ([dic valueForKey:NORMAL]) {
                cell.text.text = [NSString stringWithFormat:@"%@", [dic valueForKey:NORMAL]];
                
            }else{
                cell.text.text = @"";
            }

        }
    }else if([fieldtype isEqualToString:@"4"]){ //*** 数据字典
        
        cell.startDay.hidden = YES;
        cell.endDay.hidden = YES;
        cell.lineImg.hidden = YES;

        cell.text.enabled = NO;
    }else if([fieldtype isEqualToString:@"1"]){ //*** 数字
        [cell updateLayOutForNum];
        
        cell.startDay.hidden = YES;
        cell.endDay.hidden = YES;
        cell.lineImg.hidden = YES;

        if ([dic valueForKey:LEFT]) {
            cell.textLeft.text = [NSString stringWithFormat:@"%@", [dic valueForKey:LEFT]];
        }else{
            cell.textLeft.text = @"";
        }
        
        if ([dic valueForKey:RIGHT]) {
            
            if (indexPath.row == 1) {
                NSLog(@"3123");
            }

            cell.textRight.text = [NSString stringWithFormat:@"%@", [dic valueForKey:RIGHT]];
        }else{
            if (indexPath.row == 1) {
                NSLog(@"3123");
            }
            cell.textRight.text = @"";
        }

    }else{ // *** NORMAL
    
        cell.text.enabled = YES;
        [cell updateLayOutForNormal];
        
        if ([dic valueForKey:NORMAL]) {
            cell.text.text = [NSString stringWithFormat:@"%@", [dic valueForKey:NORMAL]];
            
        }else{
            cell.text.text = @"";
        }

    }
    
    
    if (shouldClear) {
        cell.text.text = @"";
    }

    if (indexPath.row == _fieldArr.count-1) {
        shouldClear = NO;
    }

    if([fieldtype isEqualToString:@"1"]){ //*** 数字
        cell.contentChange = NO;
    }else{
        cell.contentChange = YES;
    }
    
    cell.textLeft.delegate = self;
    cell.textRight.delegate = self;
    
    
//    cell.text.text = @"dasda";
    return cell;
}

-(void)rightBtnClick1:(NSString *)str{
    
    NSDictionary *dicHaveSee = [_arrSelected objectAtIndex:_indexNum];
    
    NSLog(@"%ld",_indexNum);
     SearchCell *cell= (SearchCell *)[self.tableView cellForRowAtIndexPath:_indexPath];//(UITextField *)[self viewWithTag:1000+_indexNum];
    UITextField *textField = cell.text;

    NormalButton *start = (NormalButton *)[self viewWithTag:2000+_indexNum];
    NormalButton *end = (NormalButton *)[self viewWithTag:3000+_indexNum];
    
    

    NSLog(@"%@", _seleView.seleTitle);

        textField.text = _seleView.seleTitle;
    
    NSDictionary *dic = [_arrSelected objectAtIndex:_indexPath.row];
    [dic setValue:textField.text forKey:NORMAL];

    NSLog(@"%@", _seleView.seleTitle);
    
        _seleView.hidden = YES;
        _seleViewBg.hidden = _seleView.hidden;

        NSDictionary *d = [_fieldArr objectAtIndex:_indexNum];
        if(![[NSString stringWithFormat:@"%@",[d objectForKey:@"fieldRole"]]isEqualToString:@"16"]&&![d valueForKey:@"ifPlug"]){
        NSString *p1 = [NSString stringWithFormat:@"%@_%@_andOr",[d objectForKey:@"fieldAliasName"],[d objectForKey:@"fieldType"]];
        NSString *p2 = [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:[d objectForKey:@"fieldType"]]];
       
        NSString *p3 = [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:[d objectForKey:@"fieldType"]]];
            
            [_param setObject:@"0" forKey:p1];
            if(![_param valueForKey:p2])[_param setObject:@"0" forKey:p2];
            if(_seleView.seleTitleNum)
                [_param setObject:_seleView.seleTitleNum forKey:p3];
            
           if ([[NSString stringWithFormat:@"%@",[d objectForKey:@"fieldType"]]isEqualToString:@"3"]){
               
              if([_param objectForKey:p2] == nil) [_param setObject:@"" forKey:p2];
                if([_param objectForKey:p3] == nil)[_param setObject:@"" forKey:p3];

               if (currentBtn&&str.length>0) {
                   [currentBtn setTitle:str forState:UIControlStateNormal];
               }
               if(start.titleLabel.text&&currentBtn == start){
                   
                   [dicHaveSee setValue:[NSString stringWithFormat:@"%@", start.titleLabel.text] forKey:LEFT];

               [_param setObject:start.titleLabel.text forKey:p2];
                   if([str isEqualToString:@""])[_param removeObjectForKey:p2];

               }
               if(end.titleLabel.text&&currentBtn == end){
                   
                   [dicHaveSee setValue:[NSString stringWithFormat:@"%@", end.titleLabel.text] forKey:RIGHT];
                   
                   [_param setObject:end.titleLabel.text forKey:p3];
                   if([str isEqualToString:@""])[_param removeObjectForKey:p3];

               }
            }
       
            _seleView.seleTitle = @"";
            _seleView.seleTitleNum = @"";
        }
    
        else{
            
            NSString *p1 = [NSString stringWithFormat:@"%@_d_andOr",[d objectForKey:@"fieldAliasName"]];
//            NSString *p2 = [NSString stringWithFormat:@"%@strCond_dic",[d objectForKey:@"fieldName"]];
            NSString *p2 = [NSString stringWithFormat:@"%@strCond_dic",[d objectForKey:@"fieldAliasName"]];

//            NSString *p3 = [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:[d objectForKey:@"fieldType"]]];
            if(![_param valueForKey:p1])[_param setObject:@"0" forKey:p1];
//            [_param setObject:@"0" forKey:p2];
            [_param setObject:_seleView.seleTitleNum forKey:p2];
//            [_dicArr replaceObjectAtIndex:_indexNum withObject:_seleView.seleTitle];
            
            _seleView.seleTitle = @"";
            _seleView.seleTitleNum = @"";

            [self.tableView reloadData];
        }
    [_seleView.selectManyArr removeAllObjects];
    
    timeSelectView.hidden = YES;
    _timeSelectViewBg.hidden = timeSelectView.hidden;

    
    NSLog(@"%@", _seleView.seleTitle);

        NSLog(@"%@",_param);
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    SearchCell *cell = (SearchCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    //*** make the last textField resignFirstResponce to make the UI
    if(lastTextField)[lastTextField resignFirstResponder];
    
//    UITextField *start = (UITextField *)[self viewWithTag:2000+indexPath.row];
//    UITextField *end = (UITextField *)[self viewWithTag:3000+indexPath.row];
    _oneText = cell.text;//(UITextField *)[self viewWithTag:1000+indexPath.row];

    NSDictionary *d = [_fieldArr objectAtIndex:indexPath.row];
    NSString *fieldRole=[NSString stringWithFormat:@"%@",[d objectForKey:@"fieldRole"]];
    NSString *fieldType=[NSString stringWithFormat:@"%@",[d objectForKey:@"fieldType"]];

    _indexNum = indexPath.row;
    _indexPath = indexPath;
    if([fieldRole isEqualToString:@"16"]||[fieldType isEqualToString:@"4"]){
        _seleView.hidden = NO;
        ;
        _seleViewBg.hidden = _seleView.hidden;

//        NSString *jsonStr = [d objectForKey:@"dicOptions"];
//        NSData *data =[jsonStr dataUsingEncoding:NSUTF8StringEncoding];
//        NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        _seleView.dataArr = [NSArray arrayWithArray:arr];
        
        _seleView.dataArr = [NSArray arrayWithArray:[d objectForKey:@"dicList"]];

//        if(arr.count<=3){
//            _seleView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-150, 40,arr.count*50 , 300);
//        }
        
    }else if(![fieldType isEqualToString:@"1"]){ // 排除左右选择
        [_oneText becomeFirstResponder];
    }
//    else if([fieldRole isEqualToString:@"14"]||[fieldRole isEqualToString:@"15"]||[fieldRole isEqualToString:@"26"]||[fieldRole isEqualToString:@"28"]){
//        timeSelectView.hidden = NO;
//        _timeSelectViewBg.hidden = timeSelectView.hidden;
//
//        [timeSelectView startAnima];
//
//    }
}

#pragma nark - UITestFieldDelegate
- (void)textFieldDidBeginEditing:(NormalText *)textField{
    lastTextField = textField;
    
}
-(void)textFieldDidEndEditing:(NormalText *)textField{
    
    if (textField.text.length<=0) {
        return;
    }
    NSInteger i =textField.indexPath.row;
    NSDictionary *d = [_fieldArr objectAtIndex:i];
    NSDictionary *dic = [_arrSelected objectAtIndex:i];

    NSString *p3;
    NSString *pForPlaceHo;      // text Value numVal
    NSString *pForNumClud;     // numCond
    NSString *pForNumPlaceHo;  // numCondPlace

     // AFM_22_numCondOne_pld=2&AFM_22_numValOne_pld=1&AFM_22_1_andOr=0&AFM_22_numCondTwo_pld=2&AFM_22_numValTwo_pld=22
    
    
//    AFM_22_numCondOne_pld=0&
//                                AFM_22_numValOne_pld=1&
//                                AFM_22_1_andOr=0&
//    AFM_22_numCondTwo_pld=0
//    &
//                                AFM_22_numValTwo_pld=55
//    http://192.168.6.150:8081/edus_auto/model_interfaceToList.do?&password=000000&loginName=admin&tableId=15&pageId=6&
//    AFM_22_numCondOne_pld=2&
//                                AFM_22_numValOne_pld=1&
//                                AFM_22_1_andOr=0&
//    AFM_22_numCondTwo_pld=2&
//                                AFM_22_numValTwo_pld=22
//    
    if(![textField isKindOfClass:[CustomTextField class]]){
        
        p3= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:[d objectForKey:@"fieldType"]]];   // 平常情况下 ， 左边墙框下 键
        if([[d objectForKey:@"fieldType"] integerValue] == 1) {
        
            pForPlaceHo= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:@"99999"]]; // 右边墙框下键
        }

    }else{
        
        pForPlaceHo= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:[d objectForKey:@"fieldType"]]];

        p3= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr2:@"99999"]];
    }
    [_param setObject:textField.text forKey:p3];
    
    if ([NSString stringWithFormat:@"%@", [_param valueForKey:pForPlaceHo]].length==0||[[NSString stringWithFormat:@"%@", [_param valueForKey:pForPlaceHo]]isEqualToString:@"(null)"]) {
        if(pForPlaceHo)[_param setObject:@"" forKey:pForPlaceHo];
    }
    NSString *p1 = [NSString stringWithFormat:@"%@_%@_andOr",[d objectForKey:@"fieldAliasName"],[d objectForKey:@"fieldType"]];
//    NSString *p2 = [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:[d objectForKey:@"fieldType"]]];
    
//    http://192.168.6.150:8081/edus_auto/model_ajaxList.do?&tableId=15&mainTableId=&mainPageId=&mainId=&pageId=6&AFM_1_2_andOr=0&AFM_1_strCond_pld=0&0&AFM_2_strCond_pld=0&AFM_2_strVal_pld=&AFM_11_3_andOr=0&AFM_11_dateType=yyyy-MM-dd&AFM_11_startDates_pld=&AFM_11_endDates_pld=&AFM_20_2_andOr=0&AFM_20_strCond_pld=0&AFM_20_strVal_pld=&AFM_21_numCondOne_pld=2&AFM_21_numValOne_pld=&AFM_21_1_andOr=0&AFM_21_numCondTwo_pld=2&AFM_21_numValTwo_pld=&AFM_22_numCondOne_pld=2&AFM_22_numValOne_pld=1&AFM_22_1_andOr=0&AFM_22_numCondTwo_pld=2&AFM_22_numValTwo_pld=22&AFM_23_numCondOne_pld=2&AFM_23_1_andOr=0&AFM_23_numCondTwo_pld=2&AFM_23_numValTwo_pld=&defaultSearchSetSelect=&AFM_11_3_andOr=0&AFM_11_startDates_pld=&AFM_1_2_andOr=0&AFM_1_strCond_pld=&AFM_1_strVal_pld=&AFM_20_2_andOr=0&AFM_20_strCond_pld=&AFM_20_strVal_pld=&AFM_21_1_andOr=0&AFM_21_numCondOne_pld=&AFM_21_numCondTwo_pld=&AFM_21_numValOne_pld=&AFM_21_numValTwo_pld=&AFM_22_1_andOr=0&AFM_23_1_andOr=0&AFM_2_2_andOr=0&_time_stamp_=1465351006433&limit=20_flow&start=0
    
    
    if([[d objectForKey:@"fieldType"] integerValue] == 1&&![textField isKindOfClass:[CustomTextField class]]){
        
        
        [dic setValue:textField.text forKey:LEFT];
        
        pForNumClud= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:[d objectForKey:@"fieldType"]]];
        pForNumPlaceHo= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:@"5"]];

    }else if([[d objectForKey:@"fieldType"] integerValue] == 1&&[textField isKindOfClass:[CustomTextField class]]){
        
        [dic setValue:textField.text forKey:RIGHT];
        
        pForNumClud= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:@"5"]];
        pForNumPlaceHo= [NSString stringWithFormat:@"%@%@",[d objectForKey:@"fieldAliasName"],[self typeStr:[d objectForKey:@"fieldType"]]];

    }else{
    [dic setValue:textField.text forKey:NORMAL];
    }

    if(pForNumClud){
        if(![_param valueForKey:pForNumClud])[_param setObject:@"0" forKey:pForNumClud];
    }
    
    if(pForNumPlaceHo){
        if(![_param valueForKey:pForNumPlaceHo])[_param setObject:@"0" forKey:pForNumPlaceHo];
    }

    //192.168.6.150:8081/edus_auto/model_interfaceToList.do?AFM_22_numValTwo_pld=&AFM_21_numValTwo_pld=12&AFM_22_numCondOne_pld=0&loginName=admin&AFM_21_1_andOr=0&password=000000&AFM_22_numValOne_pld=&tableId=15&pageId=6&source=1&AFM_22_1_andOr=0
    if([[d objectForKey:@"fieldType"] integerValue] != 1){
    
        [_param setObject:@"0" forKey:p1];
    }else{
        [_param setObject:@"2" forKey:p1];
    }
//    [_param setObject:@"0" forKey:p2];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"%ld",textField.tag);
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - this is the time btn click to select the time we @selector
-(void)timeSelectBtnClick:(NormalButton *)btn{
    currentBtn = btn;
    timeSelectView.hidden = NO;
    _timeSelectViewBg.hidden = timeSelectView.hidden;

    [timeSelectView startAnima];
    _indexNum = btn.indexPath.row;


}
-(void)startAnima{
    
    _backBtn.hidden = NO;
    _clearBtn.hidden = NO;
    _tableView.hidden = NO;
    _bottom.hidden = NO;
    _line.hidden = NO;
    _bottomView.hidden = NO;

    [_backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/2+10));
        make.top.equalTo(self.mas_bottom).offset(-45);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/2-20));
        make.bottom.equalTo(self.mas_bottom).offset(-10);
        make.height.equalTo(@(40));
    }];
    
    [_clearBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(_backBtn.mas_top);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/2-20));
        make.bottom.equalTo(_backBtn.mas_bottom);
        make.height.equalTo(@(40));
    }];
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
        make.bottom.equalTo(self.mas_bottom).offset(-55);
    }];
//    [_bottom mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/2-17.5/2));
//        make.top.equalTo(@(350-45+55-100));
//        make.width.equalTo(@(35/2));
//        make.height.equalTo(@(18/2));
//    }];
//    [_line mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@(0));
//        make.top.equalTo(@(350-45+94-100));
//        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
//        make.height.equalTo(@(1));
//    }];
    
    
//    [_bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@(0));
//        make.top.equalTo(@(350-45+55-100));
//        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
//        make.height.equalTo(@(40));
//    }];

    [UIView animateWithDuration:0.5 animations:^{
        [self layoutIfNeeded];

//        _bottomView.frame = CGRectMake(0, 350-45+55-100, [UIScreen mainScreen].bounds.size.width, 40);


//         _backBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2+10, 350-45-100, [UIScreen mainScreen].bounds.size.width/2-20, 40);
//        _clearBtn.frame = CGRectMake(10, 350-45-100, [UIScreen mainScreen].bounds.size.width/2-20, 40);
//        _tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300);
//        _bottom.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-17.5/2, 350-45+55-100, 35/2, 18/2);
//        _line.frame = CGRectMake(0, 350-45+94-100, [UIScreen mainScreen].bounds.size.width, 1);




    }completion:^(BOOL finished) {
        
    }];
    
}

-(void)endAnima{
    
    [_backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/2+10));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/2-20));
        make.height.equalTo(@(0));
    }];
    
    [_clearBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/2-20));
        make.height.equalTo(@(0));
    }];
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
        make.height.equalTo(@(0));
    }];
    [_bottom mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/2-17.5/2));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.width.equalTo(@(35/2));
        make.height.equalTo(@(0));
    }];
    [_line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
        make.height.equalTo(@(0));
    }];
    
    [_bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(TABLEVIEWHEADERHEIGHT));
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
        make.height.equalTo(@(0));
    }];

    [UIView animateWithDuration:0.2 animations:^{
        timeSelectView.hidden = YES;
        _timeSelectViewBg.hidden = timeSelectView.hidden;
        
        _seleView.hidden = YES;
        _seleViewBg.hidden = _seleView.hidden;
        timeSelectView.hidden = YES;
        _timeSelectViewBg.hidden = timeSelectView.hidden;
        
        [self layoutIfNeeded];

    } completion:^(BOOL finished) {
        _backBtn.hidden = YES;
        _clearBtn.hidden = YES;
        _tableView.hidden = YES;
        _bottom.hidden = YES;
        _line.hidden = YES;
        _bottomView.hidden = YES;

    }];
//    [UIView animateWithDuration:0.2 animations:^{
//        timeSelectView.hidden = YES;
//        _timeSelectViewBg.hidden = timeSelectView.hidden;
//
//        _seleView.hidden = YES;
//        _seleViewBg.hidden = _seleView.hidden;
//        timeSelectView.hidden = YES;
//        _timeSelectViewBg.hidden = timeSelectView.hidden;
//
//        [self layoutIfNeeded];
//        
//
////        _bottomView.frame = CGRectMake(0, -40, [UIScreen mainScreen].bounds.size.width, 40);
//
////        _backBtn.frame = CGRectMake(100, -50, [UIScreen mainScreen].bounds.size.width-110, 40);
////        _clearBtn.frame = CGRectMake(10, -50, 70, 40);
////        _tableView.frame = CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 0);
////        _bottom.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-17.5/2, -20, 35/2, 18/2);
////        _line.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1);
//
//    }];
    
}


-(NSString *)typeStr:(NSString *)type{
    NSString *str = @"";
    NSInteger typeNum = [type integerValue];
    
    if (typeNum == 99999) {
        return @"_numValTwo_pld";
    }

    switch (typeNum) {
        case 1:
           str= @"_numCondOne_pld";
            break;
        case 2:
             str= @"_strCond_pld";
            break;
        case 3:
             str= @"_startDates_pld";
            break;
        case 4:
             str= @"_numCondOne_pld";
            break;
        case 5:
            str= @"_numCondTwo_pld";
            break;
            
        default:
            break;
    }
    return str;
}

-(NSString *)typeStr2:(NSString *)type{
    NSString *str = @"";
    NSInteger typeNum = [type integerValue];
    
    if (typeNum == 99999) {
        return @"_numValTwo_pld";
    }
    switch (typeNum) {
        case 1:
            str= @"_numValOne_pld";
            break;
        case 2:
            str= @"_strVal_pld";
            break;
        case 3:
            str= @"_endDates_pld";
            break;
        case 4:
            str= @"_numCondOne_pld";
            break;
        case 999:
            str= @"_numValTwo_pld";
            break;

        default:
            break;
    }
    return str;
}


-(void)setFieldArr:(NSArray *)fieldArr{
    _fieldArr = fieldArr;
    
    _arrSelected = [[NSMutableArray alloc]init];
    for (int i = 0; i < _fieldArr.count; i++) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [_arrSelected addObject:dic];
    }

    [self.tableView reloadData];
}

-(void)setPageID:(NSString *)pageID{
    _pageID = pageID;
}

-(void)setTableID:(NSString *)tableID{
    _tableID= tableID;
}

-(void)setSearchUrl:(NSString *)searchUrl{
    _searchUrl = searchUrl;
}


-(void)userHeidden{
    if (self.seleView.hidden == NO) {
        self.seleView.hidden = YES;
//        self.seleViewBg.hidden = self.seleView.hidden;
    }else if(timeSelectView.hidden == NO){
        timeSelectView.hidden = YES;
        _timeSelectViewBg.hidden = timeSelectView.hidden;
    }
}
@end
